package models

import (
	"fmt"

	"xorm.io/xorm"
)

var beans []any

func InitModels(db *xorm.Engine) error {
	// 同步表结构
	if err := db.Sync2(
		new(User),
		new(Role),
		new(Permission),
		new(UserRole),
		new(RolePermission),
	); err != nil {
		return fmt.Errorf("failed to sync database: %v", err)
	}
	for _, v := range beans {
		db.Sync(v)
	}
	return nil
}

func RegisterModel(bean any) {
	beans = append(beans, bean)
}
